- Functions and arguments
- The structure of functions
- Using R functions: arguments
- Help file
- Getting help in R
- When you know the name of the package / function
- When you do not know the name of the package / function
Functions are the building blocks of R
Built-in or user-defined (program your own functions).
To use a function, type the function name with parentheses: mean()
Typing the name of the function without the parentheses reveals the R code of the function: sample():
Every function in R has the following structure:
Image source: Garrett Grolemund, Hands-On Programming with R, 2.6
To use a function in R, you need to know the required input information = arguments.
For example the function sample()
Use args(<function name>) to obtain info about the arguments and the default values:
args(sample)
Or make use of the pop-up help and use the TAB key to cycle through the arguments:
Clicking F1 opens the help file of the function sample():
Clicking F1 opens the help file of the function sample():
Now we can use the sample() function to, for example, mimic the sampling of two dice.
dice <- sample(x=1:6, size=2, replace=TRUE) dice
x represents the items to sample from the range of possible items: the numbers 1 to 6 (the eyes of a single die).
size is the number of items to choose, in this case 2
replace=TRUE means sampling with replacement (throwing double 4 is allowed)
Recommendation: type out the arguments and their values. This prevents errors and increases the readability of your code.
For example, we want to load the package bayesforecast but it is not installed.
In the console, we will get the following Error:
In the RMarkdown file you get an error as well and knitting is stopped:
Calling the data set “gapminder” when the package gapminder is not loaded will produce an error and the code will not run:
data(gapminder)
Note: the error message does not mention that the package is not loaded.
Solution:
library(gapminder) data(gapminder)
Functions filter() and lag() occur in both packages dplyr and stats (basic installation).
Loading the package tidyverse will cause the filter() and lag() functions to overwrite the filter() and lag() functions of the stats package. See below: “Conflicts”
Solution? Use namespaces :: to indicate which function you want to use:
dplyr::filter()stats::filter()Every package and function in R has a help file.
You can ask for help in the search bar of the output pane.
See below an example of searching help for “plot”:
You can also ask for help in the console:
For a function: type: help(sample) or ?sample (opens a help window).
For a package type: help(package=ggplot2).
When you start typing sample in the console or editor (Markdown code chunk) a pop-up window appears with help about the structure of the function.
Some packages have cheat sheets, see in R Studio, Help menu -> Cheat Sheets
In the console:
?? followed by your search term.??anova returns a list of all help pages that contain the word ‘anova’.Google the search term(s) and add ‘R’ as keyword.
Helpful websites: http://www.stackoverflow.com and http://www.stackexchange.com
#) to clarify what you are doing
R-files
RStudio projects and a logical folder structure (data, results, figures)Solution
The file “1b Lab.html” provides help about the code: the code of the answers is given but hidden by default. If you want to see the code, click on the “SHOW” button in the right margin.